<?php

namespace app\index\controller;

use think\Controller;
use think\Request;

class Lists extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $firstCateId = [];
        //获取一级分类
        $cateFir = model('category')->getNormalFirstCategory();
        //遍历一级分类
        foreach ($cateFir as $cat) {
            $firstCateId[] = $cat->id;
        }
        //获取id值
        $id = input('id', 0, 'intval');

        $data['city_id'] = $this->city->id;
        //判断接收的数据是否是一级分类
        if (in_array($id, $firstCateId)) {
            $categoryParentId = $id;
            $data['category_id'] = $id;
        } elseif ($id) {
            //二级分类
            $category = model('category')->get($id);
            if (!$category || $category->status != 1) {
                $this->error('数据不合法');
            }
            $categoryParentId = $category->parent_id;
            $data['category_id'] = $categoryParentId;
            $data['se_category_id'] = $id;
        } else {
            //条件不满足
            $categoryParentId = 0;
        }
        //获取父类下的所有子栏目
        $secategoryid = [];
        if ($categoryParentId) {
            $secategoryid = model('category')->getNormalCategorysByParentId($categoryParentId);
        }
        //获取参数进行数据显示
        $orderflag = '';
        $order = [];
        $order_sales = input('order_sales', '');
        $order_price = input('order_price', '');
        $order_time = input('order_time', '');
        if (!empty($order_sales)) {
            $orderflag = 'order_sales';
            $order['buy_count'] = 'desc';
        } elseif (!empty($order_price)) {
            $orderflag = 'order_price';
            $order['current_price'] = 'desc';
        } elseif (!empty($order_time)) {
            $orderflag = 'order_time';
            $order['create_time'] = 'desc';
        }

        //进行数据查询
        $res = model('deal')->getDealByCategoryAndOrder($data, $order);
        return $this->fetch('', ['res' => $res, 'orderflag' => $orderflag, 'id' => $id, 'cateFir' => $cateFir, 'categorys' => $categoryParentId, 'secategory' => $secategoryid]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
